<template>
	<view>
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#f83d38" ref="tabs" :list="list" :current="current" @change="change"
					:is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition"
				@animationfinish="animationfinish">
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<block v-if="orderList.length == 0">
								<view>
									<view class="centre">
										<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode="">
										</image>
										<view class="explain">
											您还没有相关的订单
										</view>
									</view>
								</view>
							</block>
							<block v-else>
								<view class="order" v-for="(order, index) in orderList" :key="order.id">
									<view @click="orderDatail(order)">
										<view class="item" v-for="(item, index) in order.kuybOrderChanpList"
											:key="index">
											<view class="left">
												<image :src="item.chanpzt" mode="aspectFill"></image>
											</view>
											<view class="content">
												<view>{{ order.dingdbh.substr(0, 20) }}</view>
												<view>{{ item.chanpmc }}</view>
												<view>{{ item.chanpms }}</view>
												<view class="type">{{order.createTime}}</view>
											</view>
										</view>
										<view class="total">
											<text class="total-price">
												￥{{ order.dingdje / 100 }}
											</text>
										</view>
									</view>

									<view class="bottom border-top-red">
										<view class="evaluate btn" @click="payment(order)">支付</view>
									</view>
								</view>
							</block>
							<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>

				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<block v-if="orderList.length == 0">
								<view>
									<view class="centre">
										<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode="">
										</image>
										<view class="explain">
											您还没有相关的订单
										</view>
									</view>
								</view>
							</block>
							<block v-else>
								<view class="order" v-for="(order, index) in orderList" :key="order.id">
									<view @click="orderDatail(order)">
										<view class="item" v-for="(item, index) in order.kuybOrderChanpList"
											:key="index">
											<view class="left">
												<image :src="item.chanpzt" mode="aspectFill"></image>
											</view>
											<view class="content">
												<view>{{ order.dingdbh.substr(0, 20) }}</view>
												<view>{{ item.chanpmc }}</view>
												<view>{{ item.chanpms }}</view>
												<view class="type">{{order.createTime}}</view>
											</view>
										</view>
										<view class="total">
											<text class="total-price">
												￥{{ order.dingdje / 100 }}
											</text>
										</view>
									</view>

									<view class="bottom border-top-red">
										<view class="evaluate btn" @click="refund(order)">申请退款</view>
										<!-- <view class="evaluate btn" @click="confirm(order)">确认完成</view> -->
									</view>
								</view>
							</block>
							<u-loadmore :status="loadStatus[0]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>

				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;">
						<view class="page-box">
							<block v-if="orderList.length == 0">
								<view>
									<view class="centre">
										<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode="">
										</image>
										<view class="explain">
											您还没有相关的订单
										</view>
									</view>
								</view>
							</block>
							<block v-else>
								<view class="order" v-for="(order, index) in orderList" :key="order.id">
									<view @click="orderDatail(order)">
										<view class="item" v-for="(item, index) in order.kuybOrderChanpList"
											:key="index">
											<view class="left">
												<image :src="item.chanpzt" mode="aspectFill"></image>
											</view>
											<view class="content">
												<view>{{ order.dingdbh.substr(0, 20) }}</view>
												<view class="title u-line-2">{{ item.chanpmc }}</view>
												<view class="type">{{order.createTime}}</view>
											</view>
										</view>
										<view class="total">
											<text class="total-price">
												￥{{ order.dingdje / 100 }}
											</text>
										</view>
									</view>
								</view>
							</block>
							<u-loadmore :status="loadStatus[1]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
				
				<swiper-item class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;">
						<view class="page-box">
							<block v-if="orderList.length == 0">
								<view>
									<view class="centre">
										<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode="">
										</image>
										<view class="explain">
											您还没有相关的订单
										</view>
									</view>
								</view>
							</block>
							<block v-else>
								<view class="order" v-for="(order, index) in orderList" :key="order.id">
									<view @click="orderDatail(order)">
										<view class="item" v-for="(item, index) in order.kuybOrderChanpList"
											:key="index">
											<view class="left">
												<image :src="item.chanpzt" mode="aspectFill"></image>
											</view>
											<view class="content">
												<view>{{ order.dingdbh.substr(0, 20) }}</view>
												<view class="title u-line-2">{{ item.chanpmc }}</view>
												<view class="type">{{order.createTime}}</view>
											</view>
										</view>
										<view class="total">
											<text class="total-price">
												￥{{ order.dingdje / 100 }}
											</text>
										</view>
									</view>
								</view>
							</block>
							<u-loadmore :status="loadStatus[1]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>

			</swiper>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				orderList: [],
				dataList: [],
				list: [{
						name: '待付款'
					},
					{
						name: '已付款'
					},
					{
						name: '退款中'
					},
					{
						name: '已完成'
					}
				],
				current: 0,
				swiperCurrent: 0,
				tabsHeight: 0,
				dx: 0,
				loadStatus: ['loadmore', 'loadmore', 'loadmore', 'loadmore'],
			};
		},
		onLoad() {
			this.getOrderList(0);
		},
		computed: {
			// 价格小数
			priceDecimal() {
				return val => {
					if (val !== parseInt(val)) return val.slice(-2);
					else return '00';
				};
			},
			// 价格整数
			priceInt() {
				return val => {
					if (val !== parseInt(val)) return val.split('.')[0];
					else return val;
				};
			}
		},
		methods: {
			// 退款
			refund(order) {
				let that = this

				uni.showModal({
					title: '提示',
					content: '确认执行么?',
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');

							uni.request({
								url: getApp().globalData.serviceUrl + "/weixin/refund",
								data: {
									orderId: order.id
								},
								header: {
									"X-Access-Token": uni.getStorageSync("token")
								},
								method: 'GET',
								dataType: 'json',
								success: (resp) => {
									if (resp.data.success)
										that.getOrderList(1);
									else {
										uni.showToast({
											title: resp.data.message,
											icon: 'none',
											duration: 3000
										});
									}
								}
							});

						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},

			// 已支付确认完成
			confirm(order) {
				let that = this

				uni.showModal({
					title: '提示',
					content: '确认执行么?',
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');

							uni.request({
								url: getApp().globalData.serviceUrl +
									"/order/kuybOrder/changeOrderStatus",
								data: {
									id: order.id,
									dingdzt: 3
								},
								header: {
									"X-Access-Token": uni.getStorageSync("token")
								},
								method: 'PUT',
								dataType: 'json',
								success: (resp) => {
									if (resp.data.success)
										that.getOrderList(1);
									else {
										uni.showToast({
											title: resp.data.message,
											icon: 'none',
											duration: 3000
										});
									}
								}
							});

						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},

			orderDatail(order) {
				uni.setStorageSync("selectedOrder", order)
				uni.navigateTo({
					url: '../../pages2/orderDetail/orderDetail'
				})
			},

			payment(order) {
				let that = this

				uni.requestSubscribeMessage({
					tmplIds: ['LWpG_Z7ky9l4qW5N9VaCcoVhlfuS18PY_q_MF3oIscw'],
					success(res) {
						uni.request({
							url: getApp().globalData.serviceUrl + "/weixin/getPrepayIdAndPaySignByDingdbh",
							data: {
								dingdbh: order.dingdbh
							},
							header: {
								"X-Access-Token": uni.getStorageSync("token")
							},
							method: 'GET',
							dataType: 'json',
							success: (resp) => {
								if (resp.data.success) {
									uni.requestPayment({
										provider: 'wxpay',
										timeStamp: resp.data.result.timeStamp,
										nonceStr: resp.data.result.nonceStr,
										package: 'prepay_id=' + resp.data.result.prepay_id,
										signType: 'RSA',
										paySign: resp.data.result.paySign,

										success: function(res) {
											console.log('success:' + JSON.stringify(res));
											that.getOrderList(0)

										},
										fail: function(err) {
											console.log('fail:' + JSON.stringify(err));
										}
									});

								} else {
									uni.showToast({
										title: resp.data.message,
										icon: 'none',
										duration: 3000
									});
								}
							}
						})
					}
				})
			},

			reachBottom() {
				// 此tab为空数据
				// if (this.current != 2) {
				// 	this.loadStatus.splice(this.current, 1, "loading")
				// 	setTimeout(() => {
				// 		this.getOrderList(this.current);
				// 	}, 1200);
				// }
				console.log("滚动到底部刷新下一页")
			},
			
			// 页面数据
			getOrderList(idx) {
				let that = this
				that.orderList = []

				// idx 0 待付款；1已付款；2退款中；3已完成
				uni.request({
					url: getApp().globalData.serviceUrl + "/order/kuybOrder/list",
					data: {
						dingdzt: idx,
						column: "createTime",
						order: "desc",
						from: 0
					},
					header: {
						"X-Access-Token": uni.getStorageSync("token")
					},
					method: 'GET',
					success: (resp) => {
						if (resp.data.success) {
							that.orderList = resp.data.result.records

						} else {
							uni.removeStorage({
								key: 'openId',
								success: function(res) {
									console.log('success');
								}
							});
							uni.removeStorage({
								key: 'userInfo',
								success: function(res) {
									console.log('success');
								}
							});
							uni.removeStorage({
								key: 'token',
								success: function(res) {
									console.log('success');
								}
							});

							uni.showToast({
								title: resp.data.message,
								icon: 'none'
							});
						}
					},
					complete() {
						uni.hideLoading()
					}
				});

				// for(let i = 0; i < 1; i++) {
				// 	let index = this.$u.random(0, this.dataList.length - 1);
				// 	let data = JSON.parse(JSON.stringify(this.dataList[index]));
				// 	data.id = this.$u.guid();
				// 	this.orderList[idx].push(data);
				// }

				this.loadStatus.splice(this.current, 1, "loadmore")
			},
			// 总价
			totalPrice(item) {
				let price = 0;
				item.map(val => {
					price += parseFloat(val.price);
				});
				return price.toFixed(2);
			},
			// 总件数
			totalNum(item) {
				let num = 0;
				item.map(val => {
					num += val.number;
				});
				return num;
			},
			// tab栏切换
			change(index) {
				this.swiperCurrent = index;
				this.getOrderList(index);
			},
			transition({
				detail: {
					dx
				}
			}) {
				this.$refs.tabs.setDx(dx);
			},
			animationfinish({
				detail: {
					current
				}
			}) {
				this.$refs.tabs.setFinishCurrent(current);
				this.swiperCurrent = current;
				this.current = current;
			}
		}
	};
</script>

<style>
	/* #ifndef H5 */
	page {
		height: 100%;
		background-color: #f2f2f2;
	}

	/* #endif */
</style>

<style lang="scss" scoped>
	.order {
		width: 710rpx;
		background-color: #ffffff;
		margin: 20rpx auto;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 20rpx;
		font-size: 28rpx;

		.top {
			display: flex;
			justify-content: space-between;

			.left {
				display: flex;
				align-items: center;

				.store {
					margin: 0 10rpx;
					font-size: 32rpx;
					font-weight: bold;
				}
			}

			.right {
				color: $u-type-warning-dark;
			}
		}

		.item {
			display: flex;
			margin: 20rpx 0 0;

			.left {
				margin-right: 20rpx;

				image {
					width: 200rpx;
					height: 200rpx;
					border-radius: 10rpx;
				}
			}

			.content {
				.title {
					font-size: 28rpx;
					line-height: 50rpx;
				}

				.type {
					margin: 10rpx 0;
					font-size: 24rpx;
					color: $u-tips-color;
				}

				.delivery-time {
					color: #f83d38;
					font-size: 24rpx;
				}
			}

			.right {
				margin-left: 10rpx;
				padding-top: 20rpx;
				text-align: right;

				.decimal {
					font-size: 24rpx;
					margin-top: 4rpx;
				}

				.number {
					color: $u-tips-color;
					font-size: 24rpx;
				}
			}
		}

		.total {
			margin-top: 20rpx;
			text-align: right;
			font-size: 24rpx;

			.total-price {
				font-size: 32rpx;
			}
		}

		.bottom {
			display: flex;
			margin-top: 40rpx;
			padding: 0 10rpx;
			justify-content: space-between;
			align-items: center;

			.btn {
				line-height: 52rpx;
				width: 160rpx;
				border-radius: 26rpx;
				border: 2rpx solid $u-border-color;
				font-size: 26rpx;
				text-align: center;
				color: $u-type-info-dark;
			}

			.evaluate {
				color: $u-type-warning-dark;
				border-color: $u-type-warning-dark;
			}
		}
	}

	.centre {
		text-align: center;
		margin: 200rpx auto;
		font-size: 32rpx;

		image {
			width: 164rpx;
			height: 164rpx;
			border-radius: 50%;
			margin-bottom: 20rpx;
		}

		.tips {
			font-size: 24rpx;
			color: #999999;
			margin-top: 20rpx;
		}

		.btn {
			margin: 80rpx auto;
			width: 200rpx;
			border-radius: 32rpx;
			line-height: 64rpx;
			color: #ffffff;
			font-size: 26rpx;
			background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
		}
	}

	.wrap {
		display: flex;
		flex-direction: column;
		height: calc(100vh - var(--window-top));
		width: 100%;
	}

	.swiper-box {
		flex: 1;
	}

	.swiper-item {
		height: 100%;
	}
</style>
